package com.cloud.wms.ui.group.collect;


import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloud.core.eventBus.Event;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.core.utils.TimeUtils;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.group.contract.GroupCollectContract;
import com.cloud.wms.ui.group.entity.CollectPlaceEntity;
import com.cloud.wms.ui.group.presenter.GroupCollectPresenter;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.sort.entity.DetachBoxBean;

import butterknife.BindView;

/**
 * @Author:Chen
 * @Date: 2021-03-01 16:51
 * @Description:
 */
public class GroupCollectActivity extends BaseBarActivity<GroupCollectPresenter> implements GroupCollectContract.View {

    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.tv_collect_code)
    TextView tvCollectCode;
    @BindView(R.id.tv_scan_name)
    TextView tvScanName;
    @BindView(R.id.ll_collect_info)
    LinearLayout llCollectInfo;
    private TaskEntity taskEntity;
    private CollectPlaceEntity collectPlaceEntity;

    @Override
    protected String barTitle() {
        return "团购集货";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_sort_group_collection;
    }

    @Override
    protected void initEvent() {
        initScanText(scanText);
        taskEntity = getIntent().getParcelableExtra(IntentKey.TASK_KEY);
        taskEntity.setBeginTime(TimeUtils.getCurrentDateAndTime());
        llCollectInfo.setVisibility(View.VISIBLE);
        tvScanName.setText("推荐集货地号");
        mPresenter.getCollectionPlaceMsg(taskEntity.getMainBillID() + "");
    }


    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        if (collectPlaceEntity != null && result.equalsIgnoreCase(collectPlaceEntity.getContainerCode()))
            mPresenter.submitCollect(taskEntity, collectPlaceEntity.getContainerID());
        else
            mPresenter.checkCollect(result);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case GroupCollectPresenter.COLLECTION_PLACE:
                collectPlaceEntity = (CollectPlaceEntity) message.obj;
                tvCollectCode.setText(collectPlaceEntity.getContainerCode());
                break;
            case GroupCollectPresenter.CHECK_COLLECT_SUCCESS:
                DetachBoxBean detachBoxBean = (DetachBoxBean) message.obj;
                mPresenter.submitCollect(taskEntity, String.valueOf(detachBoxBean.getContainerID()));
                break;
            case GroupCollectPresenter.SUBMIT_COLLECT:
                EventBusUtil.sendEvent(new Event<>(EventConfig.CLOSE_REFRESH));
                EventBusUtil.sendEvent(new Event<>(EventConfig.TASK_REFRESH));
                success("集货成功！");
                this.finish();
                break;
        }
    }

    public static void start(Context context, String batchID, TaskEntity taskEntity) {
        Intent intent = new Intent(context, GroupCollectActivity.class);
        intent.putExtra(IntentKey.TASK_KEY, taskEntity);
        context.startActivity(intent);
    }
}
